#!/bin/sh /etc/rc.common                                                        
# Copyright (C) 2006 OpenWrt.org                                                
                                                                                
START=11
d() {
    echo $1 > /dev/null
}
mount_fqrouter_partition() {
    for disk in $(find /dev/sd*)
    do
        d $(mount -o noatime $disk /tmp/usb_boot 2>&1)
        if [ -f /tmp/usb_boot/fqrouter/boot ] ; then
            echo "found fqrouter in $disk"
            return
        fi
        d $(umount /tmp/usb_boot 2>&1)
    done
}
get_kernel_cmdline() {
    cmdline=$(cat /proc/cmdline)
    for arg in $cmdline
    do
            case "$arg" in
                    board=*) echo -n $arg; echo -n " " ;;
                    console=*) echo -n $arg; echo -n " " ;;
            esac
    done
}
start() {
    mkdir /tmp/usb_boot
    mount_fqrouter_partition
    if [ ! -f /tmp/usb_boot/fqrouter/boot ] ; then
        echo 'skip usb boot due to the boot file not found'
        return
    fi
    . /tmp/usb_boot/fqrouter/boot
    echo "fqrouter version: $FQROUTER_VERSION"
    if [ ! -f /tmp/usb_boot/fqrouter/$FQROUTER_VERSION.kernel ] ; then
        echo 'skip sub boot due to kernel not found'
        return
    fi
    KERNEL_COMMAND_LINE=$(get_kernel_cmdline)
    if [ -n "$KERNEL_COMMAND_LINE" ] ; then
        kexec -l /tmp/usb_boot/fqrouter/$FQROUTER_VERSION.kernel --command-line="$KERNEL_COMMAND_LINE"
    else
        echo 'no kernel command line might not boot'
        kexec -l /tmp/usb_boot/fqrouter/$FQROUTER_VERSION.kernel
    fi
    #Set the watchdog, so that the booting should complete before the watchdog restarts the system
    killall watchdog
    watchdog -T 300 -t 30 /dev/watchdog
    kexec -e
}
